package com.woniu.cq.medical.controller;


import com.woniu.cq.medical.common.DateUtils;
import com.woniu.cq.medical.common.Result;
import com.woniu.cq.medical.entity.Informed;
import com.woniu.cq.medical.service.InformedService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-09-09
 */
@RestController
@RequestMapping("/medical/informed")
@CrossOrigin("*")//解决跨域问题
@Slf4j
public class InformedController {
    @Autowired
    private InformedService informedService;

    private static String PATH = "https://klaus-1303116484.cos.ap-chongqing.myqcloud.com/";

    /**
     * 上传知情同意书
     * @return
     */
    @RequestMapping("uploadInformed")
    public Result uploadInformed(MultipartFile file){
        try {
            //将名字新增进入数据库
            String yyyyMMddHHmmssSSS = DateUtils.format();
            int random = new Random().nextInt(1000);
            //文件的名字
            String filename = file.getOriginalFilename();
            //文件后缀（如.txt .png等）
            String suffix = filename.substring(filename.lastIndexOf("."));
            //上传到阿里云上的名字
            String uploadname = yyyyMMddHHmmssSSS+random+suffix;
            //文件在阿里云的访问地址
            String url = PATH+uploadname;

            informedService.uploadInformed(file,uploadname);
            informedService.insertInformed(filename,url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success();
    }

    /**
     * 查询所有的知情同意书
     * @return
     */
    @RequestMapping("selectAllInformed")
    public Result selectAllInformed(){
        List<Informed> informeds = informedService.selectAllInformed();
        return Result.success(informeds);
    }

}

